import { componentId } from '@/utils/unique'
import cloneDeep from 'lodash/cloneDeep'

//验证是否包含中文
function validateWords(str = '') {
  return /.*[\u4e00-\u9fa5]+.*$/.test(str || '')
}
const nameList = [
  '标签',
  '提示',
  '名称',
  '消息内容',
  '下拉名称',
  '模板名称',
  '分割文案',
  '上传按钮名称',
  '请输入',
  '图表标题',
  '主题内容',
  '标题内容',
  '格式化',
  '打印机标签',
]
//保存数据
function setObjValue(target = {}, result = []) {
  // if (!target._id) {
  //   target._id = 'reNameTypeId' + '_' + componentId(6)
  //   target._ownLanguageType = 'Language'
  // }
  result.push({
    _id: target._id,
    type: target.type,
    name: target.name,
    label: target.label,
    value: target.value,
    title: target.title,
    showtext: target.showtext,
  })
}

//设置ID
function setTargetId(_target = {}, result = []) {
  //输入框value类型
  if (_target.type === 'exInput' && _target.value && typeof _target.value === 'string' && nameList.find((el) => el == _target.name)) {
    setObjValue(
      {
        _id: _target._id,
        type: _target.type,
        value: _target.value || '',
        showtext: _target.value || '',
      },
      result
    )
  }
  if (_target.type === 'uiPrint' && _target.name) {
    setObjValue(
      {
        _id: _target._id,
        type: _target.type,
        value: _target.name || '',
        showtext: _target.name || '',
      },
      result
    )
  }
  if (_target.type === 'exRequestData' && _target.name == '模板选项' && _target.value?.length) {
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].key && !validateWords(_target.value[i].key)) continue
      // if (validateWords(_target.value[i].title)) {
      setObjValue(
        {
          _id: _target.value[i]._id,
          type: _target.type,
          title: _target.value[i].key || '',
          showtext: _target.value[i].key || '',
        },
        result
      )
      // }
    }
  }
  if (_target.type === 'uiForm' && _target.rules && Object.keys(_target.rules).length > 0) {
    for (const key in _target.rules) {
      for (let index = 0; index < _target.rules[key].length; index++) {
        if (_target.rules[key][index].message) {
          setObjValue(
            {
              _id: _target.rules[key][index]._id,
              type: _target.type,
              title: _target.rules[key][index].message || '',
              showtext: _target.rules[key][index].message || '',
            },
            result
          )
        }
      }
    }
  }
  //表格类型title属性
  if ((_target.type === 'exTableColumns' || _target.type === 'exPdaTableColumns') && _target.value?.length) {
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].title) continue
      // if (validateWords(_target.value[i].title)) {
      setObjValue(
        {
          _id: _target.value[i]._id,
          type: _target.type,
          title: _target.value[i].title || '',
          showtext: _target.value[i].title || '',
        },
        result
      )
      // }
    }
  }
  if (_target.type === 'exContentSet' && _target.value && _target.value.setList.length) {
    for (let i = 0; i < _target.value.setList.length; i++) {
      if (!_target.value.setList[i].title) continue
      setObjValue(
        {
          _id: _target.value.setList[i]._id,
          type: _target.type,
          title: _target.value.setList[i].title || '',
          showtext: _target.value.setList[i].title || '',
        },
        result
      )
    }
  }
  if ((_target.type === 'exChartSeries' || _target.type === 'exChartAxis' || _target.type === 'exTableOperateMenuConfig') && _target.value.length) {
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].name) continue
      setObjValue(
        {
          _id: _target.value[i]._id,
          type: _target.type,
          title: _target.value[i].name || '',
          showtext: _target.value[i].name || '',
        },
        result
      )
    }
  }
  //可编辑的name属性
  if ((_target.VERSION || typeof _target.type === 'number') && _target.name) {
    // console.log(_target)
    setObjValue(
      {
        _id: _target._id,
        type: _target.type,
        name: _target.name,
        showtext: _target.name || '',
      },
      result
    )
  }
  if (_target.type == 'exValidateSelect' && _target.value && _target.value.message) {
    setObjValue(
      {
        _id: _target._id,
        type: _target.type,
        name: _target.value.message,
        showtext: _target.value.message || '',
      },
      result
    )
  }
  if (_target.type == 'exSwitchToolTip' && _target.value && _target.value.formatter) {
    setObjValue(
      {
        _id: _target._id,
        type: _target.type,
        name: _target.value.formatter,
        showtext: _target.value.formatter || '',
      },
      result
    )
  }
  //配置选项的label属性
  if ((_target.type === 'exSelectOptionConfig' || _target.type === 'exJsonList') && _target?.value && _target?.value?.length) {
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].label) continue
      // if (validateWords(_target.value[i].label)) {
      setObjValue(
        {
          _id: _target.value[i]._id,
          type: _target.type,
          label: _target.value[i].label || '',
          showtext: _target.value[i].label || '',
        },
        result
      )
      // }
    }
  }
  if (_target.type === 'exRequestSet' && _target.value && _target.value.resList.length > 0) {
    for (let i = 0; i < _target.value.resList.length; i++) {
      if (!_target.value.resList[i].id && !_target.value.resList[i].titleName) continue
      // setNotId(_target.value.resList[i])
      if (validateWords(_target.value.resList[i].id)) {
        setObjValue(
          {
            _id: _target.value.resList[i]._id,
            type: _target.value.resList[i]?.type,
            label: _target.value.resList[i].id || '',
            showtext: _target.value.resList[i].id || '',
          },
          result
        )
      }
      if (validateWords(_target.value.resList[i].titleName)) {
        setObjValue(
          {
            _id: _target.value.resList[i]._id,
            type: _target.value.resList[i]?.type,
            label: _target.value.resList[i].titleName || '',
            showtext: _target.value.resList[i].titleName || '',
          },
          result
        )
      }
    }
  }
  if (_target.type === 'exLegend' && _target.value.length) {
    console.log('exLegend', _target)
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].legendName) continue
      // setNotId(_target.value[i])
      setObjValue(
        {
          _id: _target.value[i]._id,
          type: _target.type,
          label: _target.value[i].legendName || '',
          showtext: _target.value[i].legendName || '',
        },
        result
      )
    }
  }
  if (_target.type === 'exDropdownOption' && _target.value.length) {
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].label) continue
      // setNotId(_target.value[i])
      setObjValue(
        {
          _id: _target.value[i]._id,
          type: _target.type,
          label: _target.value[i].label || '',
          showtext: _target.value[i].label || '',
        },
        result
      )
    }
  }
  if (_target.type === 'exSetGanttStatus' && _target.value.length) {
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].name) continue
      // setNotId(_target.value[i])
      setObjValue(
        {
          _id: _target.value[i]._id,
          type: _target.type,
          label: _target.value[i].name || '',
          showtext: _target.value[i].name || '',
        },
        result
      )
    }
  }
  //tabsList可修改的label属性
  if (_target.type === 'tabsList' && _target.label) {
    setObjValue(
      {
        _id: _target._id,
        type: _target.type,
        label: _target.label || '',
        showtext: _target.label || '',
      },
      result
    )
  }
  //递归执行对象
  for (let key in _target) {
    if (Array.isArray(_target[key])) {
      _target[key].forEach((item) => {
        toString.call(item) === '[object Object]' && setTargetId(item, result)
      })
    } else if (toString.call(_target[key]) === '[object Object]') {
      setTargetId(_target[key], result)
    }
  }
}
function setNotId(target = {}) {
  if (!target._id) {
    target._id = 'reNameTypeId' + '_' + componentId(6)
  }
}
//保存时删除图片base64,只保存FILE_SEQ，避免json过大
export function deleteImageBase64(pageInfo) {
  function deleteBase64(list) {
    for (let index = 0; index < list.length; index++) {
      const element = list[index]
      if (element.type == 'uiImage') {
        if (element.extension.backgroundSet?.value?.FILE_SEQ && element.extension.backgroundSet.value.url) {
          element.extension.backgroundSet.value.url = ''
        }
      }
      if (element.list && element.list.length > 0) {
        deleteBase64(element.list)
      }
      if (element.pageModules && element.pageModules.length > 0) {
        deleteBase64(element.pageModules)
      }
    }
  }
  if (pageInfo.list.length > 0) {
    deleteBase64(pageInfo.list)
  }
  if (pageInfo.pageModules.length > 0) {
    deleteBase64(pageInfo.pageModules)
  }
}
//翻译项ID没有，进行设置
export function setPageJsonreNameTypeId(_target) {
  if (_target.type === 'exInput' && _target.value && typeof _target.value === 'string' && nameList.find((el) => el == _target.name)) {
    setNotId(_target)
  }
  if ((_target.type === 'exSelectOptionConfig' || _target.type === 'exJsonList' || _target.type === 'exDropdownOption') && _target.value.length) {
    console.log(_target)
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].label) continue
      console.log(_target)
      // if (validateWords(_target.value[i].label)) {
      setNotId(_target.value[i])
      // }
    }
  }
  if (_target.type == 'exValidateSelect' && _target.value && _target.value.message) {
    setNotId(_target)
  }
  if (_target.type == 'exSwitchToolTip' && _target.value && _target.value.formatter) {
    setNotId(_target)
  }
  if (_target.type === 'uiForm' && _target.rules && Object.keys(_target.rules).length > 0) {
    for (const key in _target.rules) {
      for (let index = 0; index < _target.rules[key].length; index++) {
        if (_target.rules[key][index].message) {
          setNotId(_target.rules[key][index])
        }
      }
    }
  }
  if (_target.type === 'uiPrint' && _target.name) {
    setNotId(_target)
  }
  //表格类型title属性
  if (_target.type === 'exRequestData' && _target.name == '模板选项' && _target.value?.length) {
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].key && !validateWords(_target.value[i].key)) continue
      // if (validateWords(_target.value[i].title)) {
      setNotId(_target.value[i])
      // }
    }
  }
  if (_target.type === 'exLegend' && _target.value.length) {
    console.log(_target)
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].legendName) continue
      setNotId(_target.value[i])
    }
  }
  if (_target.type === 'exSetGanttStatus' && _target.value.length) {
    console.log(_target)
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].name) continue
      setNotId(_target.value[i])
    }
  }
  if ((_target.type === 'exTableColumns' || _target.type === 'exPdaTableColumns') && _target.value.length) {
    console.log(_target)
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].title) continue
      setNotId(_target.value[i])
    }
  }
  if (_target.type === 'exRequestSet' && _target.value && _target.value.resList.length > 0) {
    for (let i = 0; i < _target.value.resList.length; i++) {
      if (!_target.value.resList[i].id) continue
      setNotId(_target.value.resList[i])
    }
  }
  if (_target.type === 'exContentSet' && _target.value && _target.value.setList.length) {
    for (let i = 0; i < _target.value.setList.length; i++) {
      if (!_target.value.setList[i].title) continue
      setNotId(_target.value.setList[i])
    }
  }
  if ((_target.type === 'exChartSeries' || _target.type === 'exChartAxis' || _target.type === 'exTableOperateMenuConfig') && _target.value.length) {
    for (let i = 0; i < _target.value.length; i++) {
      if (!_target.value[i].name) continue
      setNotId(_target.value[i])
    }
  }
  for (let key in _target) {
    if (Array.isArray(_target[key])) {
      if (_target[key]._id == 'uiTitleCard_fmh93m') {
        console.log(_target[key])
      }
      _target[key].forEach((item) => {
        toString.call(item) === '[object Object]' && setPageJsonreNameTypeId(item)
      })
    } else if (toString.call(_target[key]) === '[object Object]') {
      if (_target[key]._id == 'uiTitleCard_fmh93m') {
        console.log(_target[key])
      }
      setPageJsonreNameTypeId(_target[key])
    }
  }
  return _target
}
/*
获取所有可翻译的组件
返回对象 {
  pageJsonNew: {},
  components: [],
}
pageJsonNew 新的pageJson
components 页面渲染的数组
*/
import { ungzip } from '@/utils/gzip.js'
export function getComponentsAll(target, zhcn) {
  let result_zhcn = zhcn ? ungzip(zhcn) : ''
  let target_zhcn = target ? ungzip(target) : {}

  const _target = cloneDeep(target_zhcn)
  const result = []
  // let old
  // if (!zhcn) {
  //   old = cloneDeep(setPageJsonreNameTypeId(_target))
  //   console.log(old)
  // }
  //设置ID
  console.log(_target)
  setTargetId(_target, result)
  // console.log(result)
  if (result_zhcn) {
    result.forEach((r, i) => {
      if (!r._id) r._id = result_zhcn.components[i]._id
    })
  }
  //返回结果
  return {
    // pageJsonNew: target,
    components: result,
  }
}

//合并翻译后的数据以及删除自定义属性
export function extendLanguage(target = {}, source = []) {
  const _target = target
  //获取对象
  const find = source.find((item) => item._id === _target._id)
  if (find) {
    //合并对象
    if (find.showtext) {
      if (_target.type == 'isChartRequest') {
        if (validateWords(_target.id)) {
          _target.id && (_target.id = find.showtext)
        }
        if (validateWords(_target.titleName)) {
          _target.titleName && (_target.titleName = find.showtext)
        }
      } else if (nameList.find((el) => el == _target.name)) {
        _target.label && (_target.label = find.showtext)
        _target.value && (_target.value = find.showtext)
        _target.title && (_target.title = find.showtext)
      } else if (_target.label && _target.value) {
        _target.label && (_target.label = find.showtext)
      } else if (_target.key && _target.value) {
        _target.key && (_target.key = find.showtext)
      } else if (_target.type == 'exSwitchToolTip') {
        _target.value.formatter && (_target.value.formatter = find.showtext)
      } else {
        _target.name && (_target.name = find.showtext)
        _target.label && (_target.label = find.showtext)
        _target.value && (_target.value = find.showtext)
        _target.title && (_target.title = find.showtext)
        _target.legendName && (_target.legendName = find.showtext)
        _target.message && (_target.message = find.showtext)
        // _target.formatter && (_target.formatter = find.showtext)
      }
    }

    // if (_target._ownLanguageType) {
    //   delete _target._ownLanguageType
    //   delete _target._id
    // }
  }
  //递归执行对象
  for (let key in _target) {
    if (Array.isArray(_target[key])) {
      _target[key].forEach((item) => {
        toString.call(item) === '[object Object]' && extendLanguage(item, source)
      })
    } else if (toString.call(_target[key]) === '[object Object]') {
      extendLanguage(_target[key], source)
    }
  }
}
